package com.yidu.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.action.vo.ZTree;
import com.yidu.comment.Tools;
import com.yidu.dao.SnMoRoleMapper;
import com.yidu.dao.SnRoleMapper;
import com.yidu.model.SnFinance;
import com.yidu.model.SnMoRole;
import com.yidu.model.SnRole;
import com.yidu.model.SnUser;
import com.yidu.service.SnRoleService;

/**
 * 
 * 描述:角色表服务实现类
 * @author 陈旭
 * @date 2018年11月30日
 */
@Service
public class SnRoleServiceImpl implements SnRoleService{
	
	/**
	 * 注入角色Dao
	 */
	@Resource
	private SnRoleMapper roleDao;
	/**
	 * 注入角色模块Dao
	 */
	@Resource
	private SnMoRoleMapper smrDao;
	
	@Override
	public int deleteByPrimaryKey(String roId) {
		return roleDao.deleteByPrimaryKey(roId);
	}

	@Override
	public int insert(SnRole record) {
		return roleDao.insert(record);
	}

	@Override
	public int insertSelective(SnRole record) {
		return roleDao.insertSelective(record);
	}

	@Override
	public SnRole selectByPrimaryKey(String roId) {
		return roleDao.selectByPrimaryKey(roId);
	}

	@Override
	public int updateByPrimaryKeySelective(SnRole record) {
		return roleDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(SnRole record) {
		return roleDao.updateByPrimaryKey(record);
	}

	@Override
	public List<SnRole> queryAll(Map<String, Object> map) {
		return roleDao.queryAll(map);
	}

	@Override
	public int addOrUpdate(SnRole snRole,String id) {
		//定义一个row变量
		int row = 0;
		//判断RoId 不能为空
		if(snRole.getRoId() != null && !"".equals(snRole.getRoId())){
			//调用smrDao类的deleteByPrimaryKey方法
			smrDao.deleteByPrimaryKey(snRole.getRoId());
			//获取到参数id 根据@分割
			String[] str = id.split("@");
			//创建一个角色模块表对象
			SnMoRole smr = new SnMoRole();
			for (int i = 0; i < str.length; i++) {
				//给模块ID赋值i
				smr.setMoId(str[i]);
				//给角色ID赋值
				smr.setRoId(snRole.getRoId());
				//调用smrDao增加的方法
				smrDao.insertSelective(smr);
			}
			//调角色DAO的修改方法
			row = roleDao.updateByPrimaryKeySelective(snRole);
		}else{
			//生成UUID
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			//给操作人赋值
			snRole.setOper("admin");
			//给操作时间赋值
			snRole.setOptime(Tools.getCurDateTime());
			//给角色ID赋值
			snRole.setRoId(uuid);
			//调角色DAO的增加方法
			row = roleDao.insertSelective(snRole);
			
			//获取到参数id 根据@分割
			String[] str = id.split("@");
			//创建一个角色模块表对象
			SnMoRole smr = new SnMoRole();
			//for
			for (int i = 0; i < str.length; i++) {
				//给模块ID赋值
				smr.setMoId(str[i]);
				//给角色ID赋值
				smr.setRoId(snRole.getRoId());
				//调用角色模块Dao增加的方法
				smrDao.insertSelective(smr);
			}
		}
		return row;
	}

	@Override
	public int findCount(Map<String, Object> map) {
		return roleDao.findCount(map);
	}

	@Override
	public List<ZTree> queryTree() {
		return roleDao.queryTree();
	}

	@Override
	public List<ZTree> queryTreeNode(String id) {
		return roleDao.queryTreeNode(id);
	}

	@Override
	public List<ZTree> findById(String roId) {
		return roleDao.findById(roId);
	}

	@Override
	public SnUser login(SnUser user) {
		return roleDao.login(user);
	}

	@Override
	public SnFinance findByMoney(String barId) {
		return roleDao.findByMoney(barId);
	}

	@Override
	public int findMemberCount() {
		return roleDao.findMemberCount();
	}

	@Override
	public int findOldPwd(String userId, String pwd) {
		return roleDao.findOldPwd(userId, pwd);
	}

	@Override
	public int updatePwd(String userId, String pwd) {
		return roleDao.updatePwd(userId, pwd);
	}

	@Override
	public int findComCount() {
		return roleDao.findComCount();
	}

	@Override
	public int findBraCount() {
		return roleDao.findBraCount();
	}
}
